测试链接是否成功
1 | curl https://api.openai.com/v1/models \ |
1 | from openai import OpenAI |
发送api请求示例
1 | curl https://api.openai.com/v1/chat/completions \ # 部署服务的地址 |
像模型发送上述请求后,你会得到一个完整的json格式的返回,格式如下1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27{
"id": "chatcmpl-abc123",
"object": "chat.completion",
"created": 1677858242,
"model": "gpt-4o-mini",
"usage": {
"prompt_tokens": 13,
"completion_tokens": 7,
"total_tokens": 20,
"completion_tokens_details": {
"reasoning_tokens": 0,
"accepted_prediction_tokens": 0,
"rejected_prediction_tokens": 0
}
},
"choices": [
{
"message": {
"role": "assistant",
"content": "\n\nThis is a test!"
},
"logprobs": null,
"finish_reason": "stop",
"index": 0
}
]
}
聊天
创建对话
1 | from openai import OpenAI |
对话包含图片上传
1 | from openai import OpenAI |
1 | curl https://api.openai.com/v1/chat/completions \ |
api使用音频
文字转音频
1 | from pathlib import Path |
参数说明
| 参数 | 数据类型 | 是否必须 | 说明 |
| ————— | ——– | ———————————————————————————————————- | ———————————————————————————————————————————————————————————————— |
| model | string | Required | One of the available TTS models: tts-1 or tts-1-hd |
| input | string | Required | The text to generate audio for. The maximum length is 4096 characters. |
| voice | string | Required | The voice to use when generating the audio. Supported voices are alloy, ash, coral, echo, fable, onyx, nova, sage and shimmer. Previews of the voices are available in the Text to speech guide. |
| response_format | string | Optional Defaults to mp3,The format to audio in. Supported formats are mp3, opus, aac, flac, wav, and pcm. |
| speed | number | Optional | Defaults to 1 ,The speed of the generated audio. Select a value from 0.25 to 4.0. 1.0 is the default. |
| Returns | - | - | The audio file content. |
音频转文字
1 | from openai import OpenAI |
翻译
1 | from openai import OpenAI |